﻿using System.Text;
using System.Text.RegularExpressions;
using Course;
using MvcDemo;

namespace System.Web.Mvc
{
    public enum BarStyle
    {
        yahoo, digg, meneame, flickr, sabrosus, scott, quotes, black, black2, grayr, yellow, jogger, starcraft2, tres, megas512, technorati, youtube, msdn, badoo, viciao, yahoo2, green_black
    }
    public static class PagerBarExtension
    {
        public static string PagerBar<T>(this HtmlHelper html,IPagedList<T> list) where T :class
        {
            return PagerBar(html, list, BarStyle.technorati, 5);
        }

        public static string PagerBar<T>(this HtmlHelper html,IPagedList<T> list, BarStyle style) where T :class
        {
            return PagerBar(html, list, style, 5);
        }

        public static string PagerBar<T>(this HtmlHelper html,IPagedList<T> list, BarStyle style,int show) where T :class
        {
            return BuiderBar(html, list.PageNumber, list.PageCount, style, show);
        }

        private static string BuiderBar(HtmlHelper html,int page ,int total, BarStyle style,int show)
        {
            if (total == 1)
            {
                return "";
            }
            var sb = new StringBuilder();
            var path = html.ViewContext.HttpContext.Request.Path;
            sb.Append("<div class=\"");
            sb.Append(style.ToString());
            sb.Append(" pageBar\" >");

            var queryString = html.ViewContext.HttpContext.Request.QueryString.ToString();
            if (queryString.IndexOf("p=") < 0)
            {
                queryString += "&p=" + page;
            }
            var re = new Regex(@"p=\d+", RegexOptions.IgnoreCase);
            var result = re.Replace(queryString, "p={0}");

            if (page != 1)
            {
                sb.AppendFormat("<span><a href=\"{0}\" title=\"第一页\">{1}</a></span>", path + "?" + string.Format(result, 1), "<<");
                sb.AppendFormat("<span><a href=\"{0}\" title=\"上一页\">{1}</a></span>", path + "?" + string.Format(result, page - 1), "<");
            }
            if(page>(show+1))
            {
                sb.AppendFormat("<span><a href=\"{0}\" title=\"前" + (show + 1) + "页\">{1}</a></span>", path + "?" + string.Format(result,page-(show + 1)), "..");

            }
            for (var i = page-show; i <= page+show; i++)
            {
                if (i == page)
                {
                    sb.AppendFormat("<span class=\"current\">{0}</span>", i);
                }
                else
                {
                    if (i > 0 & i<=total)
                    {
                        sb.AppendFormat("<span><a href=\"{0}\">{1}</a></span>", path + "?" + string.Format(result, i), i);
                    }
                }
            }
            if (page < (total-(show)))
            {
                sb.AppendFormat("<span><a href=\"{0}\" title=\"后" + (show + 1) + "页\">{1}</a></span>", path + "?" + string.Format(result, page + (show + 1)), "..");
            }
            if (page < total)
            {
                sb.AppendFormat("<span><a href=\"{0}\" title=\"下一页\">{1}</a></span>", path + "?" + string.Format(result, page + 1), ">");
                sb.AppendFormat("<span><a href=\"{0}\" title=\"最后一页\">{1}</a></span>", path + "?" + string.Format(result, total), ">>");
            }
            sb.AppendFormat("<span class=\"current\">共{1}页</span>", page, total);
            sb.Append("</div>");
            return sb.ToString();
        }
    }
}